package com.ruoyi.project.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.bean.ResponseResult;
import com.ruoyi.project.blog.domain.BFollowed;
import com.ruoyi.project.blog.mapper.BFollowedMapper;
import com.ruoyi.project.blog.service.IBFollowedService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
public class BFollowedServiceImpl implements IBFollowedService {

    private final BFollowedMapper followedMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult insertFollowed(String userId) {
        if (StringUtils.isBlank(userId)) {
            ResponseResult.error("被关注用户id不能为空！");
        }
        if (userId.equals(SecurityUtils.getUserId().toString())) {
            ResponseResult.error("不能关注自己哦！");
        }
        BFollowed followed = BFollowed.builder().userId(SecurityUtils.getUserId().toString()).followedUserId(userId).build();
        followedMapper.insert(followed);
        return ResponseResult.success();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult deleteFollowed(String userId) {
        if (StringUtils.isBlank(userId)) {
            throw new BusinessException("被关注用户id不能为空！");
        }
        followedMapper.delete(new LambdaQueryWrapper<BFollowed>().eq(BFollowed::getUserId, SecurityUtils.getUserId().toString()).eq(BFollowed::getFollowedUserId, userId));
        return ResponseResult.success();
    }
}
